#include "debug_log_printf.h"
#include "app_defs_types.h"
#include <stdio.h>
//#include <string.h>
//#include <time.h>
//#include <sys/time.h>
//#include <stdlib.h>
//#include <signal.h>
#include <errno.h>
//#include "hal_defs.h"
/* ------------------------------------------------------------------------------------------------
*                                            Defines
* ------------------------------------------------------------------------------------------------
*/

/* ------------------------------------------------------------------------------------------------
*                                         Global Variables
* ------------------------------------------------------------------------------------------------
*/

/* ------------------------------------------------------------------------------------------------
*										   Macros
* ------------------------------------------------------------------------------------------------
*/


/* ------------------------------------------------------------------------------------------------
*										  Prototypes
* ------------------------------------------------------------------------------------------------
*/


/*********************************************************************
 * CONSTANTS
 */

/*********************************************************************
 * MACROS
 */

/*********************************************************************
 * TYPEDEFS
 */

/*********************************************************************
 * GLOBAL VARIABLES
 */
int g_debug_level=LOG_EXCESSIVE;

/*********************************************************************
 * LOCAL VARIABLES
 */


/*********************************************************************
* LOCAL FUNCTIONS
*/


static char timeString[30]={0};	
static char cvTimeString[30]={0};

const char *date[12][2]={
{"Jan","01"},
{"Feb","02"},
{"Mar","03"},
{"Apr","04"},
{"May","05"},
{"Jun","06"},
{"Jul","07"},
{"Aug","08"},
{"Sep","09"},
{"Oct","10"},
{"Nov","11"},
{"Dec","12"}};


void spliceCompileTime(char* timeStr)
{
	timeStr = timeString;

	memcpy(timeString,__DATE__,sizeof(__DATE__));
	timeString[11] = 0x20;//" ";
	memcpy(&timeString[12],__TIME__,sizeof(__TIME__));
	vPrintf("%s\r\n",timeStr); 
}

void printCompileTime(void)
{
//	DEBUG("%s %s\r\n",__DATE__,__TIME__);
}


void printRemainHeap(const char*functionName,int line,char*a)
{	
	//vPrintf("[%s]<%d>HeapSize:%d-%s\r\n",functionName,line,xPortGetFreeHeapSize(),a);
}
void printArray(uint8_t *inbuf,uint8_t inlen)

{
	uint8_t j;
//	DEBUG("inlen=0x%02x\r\n",inlen);
	for(j=0;j<inlen;j++)
	{
//		DEBUG("%02x ",inbuf[j]);
	}
}
#if 0
/*********************************************************************
 * @fn      osal_buffer_uint32
 *
 * @brief
 *
 *   Buffer an uint32 value - LSB first.
 *
 * @param   buf - buffer
 * @param   val - uint32 value
 *
 * @return  pointer to end of destination buffer
 */
uint8* break_buffer_uint32( uint8 *buf, uint32 val )
{
  *buf++ = BREAK_UINT32( val, 0 );
  *buf++ = BREAK_UINT32( val, 1 );
  *buf++ = BREAK_UINT32( val, 2 );
  *buf++ = BREAK_UINT32( val, 3 );

  return buf;
}

/*********************************************************************
 * @fn      osal_build_uint32
 *
 * @brief
 *
 *   Build a uint32 out of sequential bytes.
 *
 * @param   swapped - sequential bytes
 * @param   len - number of bytes in the uint8 array
 *
 * @return  uint32
 */
uint32 build_uint32( uint8 *swapped, uint8 len )
{
  if ( len == 2 )
    return ( BUILD_UINT32( swapped[0], swapped[1], 0L, 0L ) );
  else if ( len == 3 )
    return ( BUILD_UINT32( swapped[0], swapped[1], swapped[2], 0L ) );
  else if ( len == 4 )
    return ( BUILD_UINT32( swapped[0], swapped[1], swapped[2], swapped[3] ) );
  else
    return ( (uint32)swapped[0] );
}

/*********************************************************************
 * @fn      osal_buffer_uint32
 *
 * @brief
 *
 *   Buffer an uint32 value - LSB first.
 *
 * @param   buf - buffer
 * @param   val - uint32 value
 *
 * @return  pointer to end of destination buffer
 */
uint8* osal_buffer_uint32( uint8 *buf, uint32 val )
{
  *buf++ = BREAK_UINT32( val, 0 );
  *buf++ = BREAK_UINT32( val, 1 );
  *buf++ = BREAK_UINT32( val, 2 );
  *buf++ = BREAK_UINT32( val, 3 );

  return buf;
}

/*********************************************************************
 * @fn      osal_buffer_uint24
 *
 * @brief
 *
 *   Buffer an uint24 value - LSB first. Note that type uint24 is
 *   typedef to uint32 in comdef.h
 *
 * @param   buf - buffer
 * @param   val - uint24 value
 *
 * @return  pointer to end of destination buffer
 */
uint8* osal_buffer_uint24( uint8 *buf, uint24 val )
{
  *buf++ = BREAK_UINT32( val, 0 );
  *buf++ = BREAK_UINT32( val, 1 );
  *buf++ = BREAK_UINT32( val, 2 );

  return buf;
}

/*********************************************************************
 * @fn      osal_build_uint32
 *
 * @brief
 *
 *   Build a uint32 out of sequential bytes.
 *
 * @param   swapped - sequential bytes
 * @param   len - number of bytes in the uint8 array
 *
 * @return  uint32
 */
uint32 osal_build_uint32( uint8 *swapped, uint8 len )
{
  if ( len == 2 )
    return ( BUILD_UINT32( swapped[0], swapped[1], 0L, 0L ) );
  else if ( len == 3 )
    return ( BUILD_UINT32( swapped[0], swapped[1], swapped[2], 0L ) );
  else if ( len == 4 )
    return ( BUILD_UINT32( swapped[0], swapped[1], swapped[2], swapped[3] ) );
  else
    return ( (uint32)swapped[0] );
}
#endif

#if 1
//#define vDBG_PRINTF()

#if (CFG_FLASH==4096)
void vDBG_ARRAY(const      char*tag,uint8_t*array,uint16_t arraylen)
{
}
#else
void vDBG_ARRAY(const      char*tag,uint8_t*array,uint16_t arraylen)
{

//	return 0;
		char p[1024]={0};
		uint16_t i;
		uint16_t stringlength,taglength,tmpIdx=0;
//		printf("begin\r\n");
//		for(i=0;i<arraylen;i++)
//		{
//			printf("%02x ",(uint8_t)array[i]);
//		}
		taglength=strlen((const char*)tag);
		stringlength = 3*arraylen + taglength +1;
//		printf("\r\nstringlength=%d\r\n",stringlength);
		snprintf((char *)p,taglength+1,"%s",tag);
		for(i=0;i<arraylen;i++)
		{
			tmpIdx = taglength+3*i;
	//		printf("tmpIdx=%d,data=%02x,i=%d\r\n",tmpIdx,data[i]);
			snprintf((char *)&p[tmpIdx],4,"%02x ",(uint8_t)array[i]);//byte+space+'\0'
	//		printf("%02x%02x%02x\r\n",p[tmpIdx],p[tmpIdx+1],p[tmpIdx+2]);
		}
		snprintf((char *)&p[tmpIdx+3],3,"\r\n");
//		printf("\r\nmiddle\r\n");
//	
//		for(i=0;i<stringlength;i++)
//		{
//			printf("%02x ",(uint8_t)p[i]);
//		}
//		printf("over\r\n");
	    vDBG_INFO(TRACE_ON,"[Interface] %s\n\r",p);
}
#endif

#endif
void printf_timestamp_ms(const char*functionname,uint32_t line)
{
	#if 0
	struct timeval tv;
	uint64_t ts;
	int res;
	res = gettimeofday(&tv, NULL);
	if(res==-1)
	{
		vDBG_ERROR("error");
		return ;
	}
	ts = (uint64_t)tv.tv_sec*1000UL + tv.tv_usec/1000UL;
	vDBG_INFO(TRACE_ON,"[%s]<%d>->%lld",functionname,line,ts);
	#endif
}
int get_system_output(char *cmd, char *output, int size)
{
  #if 0
  FILE *fp=NULL; 
    int readn,idx=0; 
    fp = popen(cmd, "r");   
    while(!feof(fp))
    {       
    	printf("size1=%d\r\n",size);
    	readn = fread(&output[idx],1, size, fp);
    	idx += readn;
        if(readn == 0)
        {      
        	if(errno == EINTR)
        	{
        		continue;
        	}
        	else
        	{
        		break;
        	}
		}
    }
//    printf("%s,size=%d\r\n",output,strlen(output));
    pclose(fp); 
		#endif
    return 0;
}


uint8_t*readline(uint8_t*buffer,uint16_t size)
{
	char * ptr = NULL,*pStr=NULL;
	int count = 0;

	//vDBG_INFO(TRACE_APP,"size=%d",size);
	pStr =ptr = (char*)buffer;
	//vDBG_INFO(TRACE_APP,"readline buffer=%s,pStr=%s",buffer,ptr);
	while ( count < size )
	{
//		vDBG_INFO(TRACE_APP,"readline count=%d,*ptr=%02x,%c",count,*ptr);
		if ((*ptr == '\n'))
		{
			break;
		}

		// increment after check for cr.  Don't want to count the cr.
		count++;
		ptr++;
	}

	// Terminate string
	*ptr = '\0';
//	vDBG_INFO(TRACE_APP,"readline count=%d,pStr=%s",count,pStr);
	// return how many bytes read.
	return (uint8_t*)pStr;
}
/*
1:little_endian
0:big_endian
*/
static bool am_little_endian ()
{
	unsigned short i=1;
	return (int)*((char *)(&i)) ? true : false;
}
#if 0
unsigned long long ntohll(unsigned long long val)
{
	 if (am_little_endian ()){
	     return (((unsigned long long )ntohl((int)((val << 32) >> 32))) << 32) | (unsigned int)ntohl((int)(val >> 32));
	 }
	 else {
	     return val;
	 }
}
unsigned long long htonll(unsigned long long val)
{
	 if (am_little_endian ()) {
	     return (((unsigned long long )htonl((int)((val << 32) >> 32))) << 32) | (unsigned int)htonl((int)(val >> 32));
	 }
	 else {
	     return val;
	 }
}
#endif	

